package phoneDataComparable;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

/**
 * @program: WCount
 * @description: 序列化测试
 * @author: Qiang.Ye
 * @create: 2019-05-26 18:54
 **/
@Getter
@Setter
@AllArgsConstructor
public class FlowBean implements WritableComparable<FlowBean> {

    /**
     * 上传流量
     */
    private     long        upFlow;

    /**
     * 下载流量
     */
    private     long        downFlow;
    /**
     * 总流量
     */
    private     long        sumFlow;

    public FlowBean() {
    }

    public FlowBean(long upFlow, long downFlow) {
        this.upFlow = upFlow;
        this.downFlow = downFlow;
        this.sumFlow = upFlow+downFlow;
    }

    public void set(long upFlow,long downFlow){
        this.upFlow = upFlow;
        this.downFlow = downFlow;
        this.sumFlow = upFlow+downFlow;
    }

    /** 
    * @Description: 写序列化方法
    * @Author: Qiang.Ye
    * @Date: 2019/5/26 
    * @param dataOutput 
    * @return: void 
    */ 
    @Override
    public void write(DataOutput dataOutput) throws IOException {
        dataOutput.writeLong(upFlow);
        dataOutput.writeLong(downFlow);
        dataOutput.writeLong(sumFlow);
    }

    /**
    * @Description:  反序列化方法
     * 反序列化方法读顺序必须和写序列化方法的写顺序必须保持一致
    * @Author: Qiang.Ye
    * @Date: 2019/5/26
    * @param dataInput
    * @return: void
    */
    @Override
    public void readFields(DataInput dataInput) throws IOException {
        upFlow = dataInput.readLong();
        downFlow = dataInput.readLong();
        sumFlow = dataInput.readLong();
    }




    @Override
    public String toString() {
        return  upFlow  +   "\t"    +   downFlow    +   "\t"    +   sumFlow;
    }


    @Override
    public int compareTo(FlowBean flowBean) {
        return this.sumFlow-flowBean.sumFlow>0?1:-1;
    }
}
